package org.little.v2;

import org.junit.Assert;
import org.junit.Test;
import org.little.beans.BeanDefinition;
import org.little.beans.PropertyValue;
import org.little.beans.factory.config.RuntimeBeanReference;
import org.little.beans.factory.support.DefaultBeanFactory;
import org.little.beans.factory.xml.XmlBeanDefinitionReader;
import org.little.core.io.ClassPathResource;

import java.util.List;

/**
 * @author Tinyspot
 * @date 2022-05-21 13:04
 */
public class BeanDefinitionTest2 {

    @Test
    public void testGetBeanDefinition() {
        DefaultBeanFactory factory = new DefaultBeanFactory();
        XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(factory);
        reader.loadBeanDefinitions(new ClassPathResource("little-v2.xml"));

        BeanDefinition beanDefinition = factory.getBeanDefinition("petStore");
        List<PropertyValue> propertyValueList = beanDefinition.getPropertyValues();
        Assert.assertTrue(propertyValueList.size() == 4);

        {
            PropertyValue propertyValue = this.getPropertyValue("accountDao", propertyValueList);
            Assert.assertNotNull(propertyValue);
            Assert.assertTrue(propertyValue.getValue() instanceof RuntimeBeanReference);
        }

        {
            PropertyValue propertyValue = this.getPropertyValue("itemDao", propertyValueList);
            Assert.assertNotNull(propertyValue);
            Assert.assertTrue(propertyValue.getValue() instanceof RuntimeBeanReference);
        }
    }

    private PropertyValue getPropertyValue(String name, List<PropertyValue> propertyValueList) {
        for (PropertyValue propertyValue : propertyValueList) {
            if (propertyValue.getName().equals(name)) {
                return propertyValue;
            }
        }
        return null;
    }
}
